000100******************************************************************
000200*PROJECT  : STU-A
000300*AUTHOR   : Minh-Tuan Ta
000400*DATE     : 03/26/2012
000500*ABSTRACT : A program to add a student record to STUMST.DAT.
000600******************************************************************
000700 IDENTIFICATION DIVISION.
000800 PROGRAM-ID. STU-A.
000900 ENVIRONMENT DIVISION.
001000     SELECT OUTFILE      ASSIGN TO "STUMST.DAT"
001100                         ORGANIZATION    IS INDEXED
001200                         ACCESS          IS RANDOM
001300                         RECORD KEY      IS OUT-STUDENT-ID
001400                         ALTERNATE KEY   IS OUT-LNAME WITH
001500                                         DUPLICATES
001600                         FILE STATUS     IS WS-STAT.
001700
001800     SELECT INZIP        ASSIGN TO "ZIPFILE.DAT"
001900                         ORGANIZATION    IS INDEXED
002000                         ACCESS          IS DYNAMIC
002100                         RECORD KEY      IS IN-ZIP
002200                         ALTERNATE KEY   IS IN-CITY WITH
002300                                         DUPLICATES
002400                         FILE STATUS     IS WS-ZIP-STAT.
002500
002600 DATA DIVISION.
002700 FILE SECTION.
002800 FD  OUTFILE.
002900 01  STUDENT-RECORD.
003000     03  OUT-STUDENT-ID      PIC 9(4).
003100     03  OUT-FNAME           PIC X(20).
003200     03  OUT-LNAME           PIC X(20).
003300     03  OUT-STREET          PIC X(30).
003400     03  OUT-ZIP             PIC X(5).
003500     03  OUT-ACTIVE          PIC X.
003600 FD  INZIP.
003700 01  IN-REC.
003800     03  IN-ZIP              PIC X(5)    VALUE SPACES.
003900     03  IN-CITY             PIC X(30)   VALUE SPACES.
004000     03  IN-STATE            PIC X(2)    VALUE SPACES.
004100     03  IN-COUNTY           PIC X(30)   VALUE SPACES.
004200 WORKING-STORAGE SECTION.
004300 01  WS-STAT             PIC XX.
004400 01  WS-ZIP-STAT         PIC XX.
004500 01  WS-EOF              PIC X VALUE 'N'.
004600 01  WS-STUDENT-ID       PIC 9(4).
004700 01  WS-FNAME            PIC X(20).
004800 01  WS-LNAME            PIC X(20).
004900 01  WS-STREET           PIC X(30).
005000 01  WS-ZIP              PIC X(5).
005100 01  WS-ACTIVE           PIC X.
005200 01  WS-DATE.
005300     03  WS-YEAR         PIC 99.
005400     03  WS-MONTH        PIC 99.
005500     03  WS-DAY          PIC 99.
005600 01  WS-ERR              PIC X VALUE 'N'.
005700 01  WS-CONT             PIC X VALUE 'Y'.
       01  WS-ADD              PIC X.
005800
005900 SCREEN SECTION.
006000 01  INPUT-SCRN.
006100         05  BLANK SCREEN.
006200         05  LINE 1  COL 01  VALUE "ADD STUDENT".
006300         05          COL 73  PIC 9(2) FROM WS-MONTH.
006400         05          COL 75  VALUE "/".
006500         05          COL 76  PIC 9(2) FROM WS-DAY.
006600         05          COL 78  VALUE "/".
006700         05          COL 79  PIC 9(2) FROM WS-YEAR.
006800         05  LINE 2  COL 37  VALUE "ADD STUDENT".
006900         05  INPUT-STUDENT-ID-SCRN.
007000             10  LINE 4  COL 20  VALUE "STUDENT ID".
007100             10          COL 40  PIC 9(4) TO WS-STUDENT-ID.
007200         05  INPUT-FNAME-SCRN.
007300             10  LINE 5  COL 20  VALUE "STUDENT FIRST NAME".
007400             10          COL 40  PIC X(20) TO WS-FNAME.
007500         05  INPUT-LNAME-SCRN.
007600             10  LINE 6  COL 20  VALUE "STUDENT LAST NAME".
007700             10          COL 40  PIC X(20) TO WS-LNAME.
007800         05  INPUT-STREET-SCRN.
007900             10  LINE 7  COL 20  VALUE "STREET".
008000             10          COL 40  PIC X(30) TO WS-STREET.
008100         05  INPUT-ZIP-SCRN.
008200             10  LINE 8  COL 20  VALUE "ZIP".
008300             10          COL 40  PIC X(5) TO WS-ZIP AUTO.
008400
008500         10  LINE 12 COL 20  VALUE "ACTIVATE (Y/N)?".
008600         05  LINE 15 COL 34  VALUE "(ENTER 0000 TO EXIT)".
008700
008800
008900 01  INPUT-WITH-ZIP-SCRN.
009000         05  BLANK SCREEN.
009100         05  LINE 1  COL 01  VALUE "ADD STUDENT".
009200         05          COL 73  PIC 9(2) FROM WS-MONTH.
009300         05          COL 75  VALUE "/".
009400         05          COL 76  PIC 9(2) FROM WS-DAY.
009500         05          COL 78  VALUE "/".
009600         05          COL 79  PIC 9(2) FROM WS-YEAR.
009700         05  LINE 2  COL 37  VALUE "ADD STUDENT".
009800         05  LINE 4  COL 20  VALUE "STUDENT ID".
009900         05          COL 40  PIC 9(4) FROM WS-STUDENT-ID.
010000         05  LINE 5  COL 20  VALUE "STUDENT FIRST NAME".
010100         05          COL 40  PIC X(20) FROM WS-FNAME.
010200         05  LINE 6  COL 20  VALUE "STUDENT LAST NAME".
010300         05          COL 40  PIC X(20) FROM WS-LNAME.
010400         05  LINE 7  COL 20  VALUE "STREET".
010500         05          COL 40  PIC X(30) FROM WS-STREET.
010600         05  LINE 8  COL 20  VALUE "ZIP".
010700         05          COL 40  PIC X(5) FROM WS-ZIP.
010800         05  LINE 9  COL 20  VALUE "CITY".
010900         05          COL 40  PIC X(30) FROM IN-CITY.
011000         05  LINE 10 COL 20  VALUE "STATE".
011100         05          COL 40  PIC X(2) FROM IN-STATE.
011200         05  LINE 11 COL 20  VALUE "COUNTY".
011300         05          COL 40  PIC X(30) FROM IN-COUNTY.
011400
011500         05  INPUT-ACTIVE-SCRN.
011600             10  LINE 12 COL 20  VALUE "ACTIVATE (Y/N)?".
011700             10          COL 40  PIC X TO WS-ACTIVE AUTO.
011800         05  LINE 15 COL 34  VALUE "(ENTER 0000 TO EXIT)".
011900 01  ERROR-SCRN.
012000     03  BLANK SCREEN.
012100     03  LINE 12 COL 32 VALUE "ERROR ADDING RECORD".
012200     03  LINE 14 COL 32 VALUE "FILE STATUS:".
012300     03          COL 46 PIC XX FROM WS-STAT.
012400     03  LINE 15 COL 32 VALUE "RECORD KEY:".
012500     03          COL 46 PIC X(8) FROM WS-STUDENT-ID.
012600     03  LINE 16 COL 32 VALUE "CONTINUE(Y/N)?".
012700     03          COL 47 PIC X TO WS-CONT AUTO.
012800
012900 01  ERROR-ZIP-SCRN.
013000     03  BLANK SCREEN.
013100     03  LINE 12 COL 32 VALUE "INVALID ZIP CODE".
013200     03  LINE 14 COL 32 VALUE "FILE STATUS:".
013300     03          COL 46 PIC XX FROM WS-ZIP-STAT.
013400     03  LINE 15 COL 32 VALUE "RECORD KEY:".
013500     03          COL 46 PIC X(8) FROM IN-ZIP.
013600     03  LINE 16 COL 32 VALUE "CONTINUE(Y/N)?".
013700     03          COL 47 PIC X TO WS-CONT AUTO.
013800
013900 01  SUCCESS-SCRN.
014000     03  BLANK SCREEN.
014100     03  LINE 12 COL 32 VALUE "RECORD ADDED SUCCESSFULLY".
014200     03  LINE 18 COL 32 VALUE "CONTINUE(Y/N)?".
014300     03          COL 47 PIC X TO WS-CONT AUTO.

       01  ADD-SCRN.
           03  BLANK SCREEN.
           03  LINE 4  COL 20  VALUE "STUDENT ID".
           03          COL 40  PIC 9(4) FROM WS-STUDENT-ID.
           03  LINE 5  COL 20  VALUE "STUDENT FIRST NAME".
           03          COL 40  PIC X(20) FROM WS-FNAME.
           03  LINE 6  COL 20  VALUE "STUDENT LAST NAME".
           03          COL 40  PIC X(20) FROM WS-LNAME.
           03  LINE 7  COL 20  VALUE "STREET".
           03          COL 40  PIC X(30) FROM WS-STREET.
           03  LINE 8  COL 20  VALUE "ZIP".
           03          COL 40  PIC X(5) FROM WS-ZIP.
           03  LINE 9  COL 20  VALUE "CITY".
           03          COL 40  PIC X(30) FROM IN-CITY.
           03  LINE 10 COL 20  VALUE "STATE".
           03          COL 40  PIC X(2) FROM IN-STATE.
           03  LINE 11 COL 20  VALUE "COUNTY".
           03          COL 40  PIC X(30) FROM IN-COUNTY.

           03  LINE 12 COL 20  VALUE "ACTIVATE".
           03          COL 40  PIC X FROM WS-ACTIVE AUTO.

           03  LINE 14 COL 32  VALUE "DO YOU WANT TO ADD THIS RECORD(Y/N
      -    ")?".
           03          COL 30  PIC X TO WS-ADD AUTO.

           01  CONTINUE-SCRN.
               03  BLANK SCREEN.
               03  LINE 9  COL 32 VALUE "DO YOU WANT TO CONTINUE(Y/N)?".
               03          COL 64 PIC X TO WS-CONT AUTO.


014600 PROCEDURE DIVISION.
014700 100-MAIN.
014800     ACCEPT WS-DATE FROM DATE
014900     OPEN    INPUT INZIP
015000             I-O   OUTFILE
015100
015200
015300     MOVE ZEROS TO WS-STUDENT-ID
015400     MOVE SPACES TO WS-FNAME
015500     MOVE ZEROS TO WS-LNAME
015600     MOVE SPACES TO WS-STREET
015700     MOVE SPACES TO WS-ZIP
015800     MOVE 'Y' TO WS-CONT
015900
016000     DISPLAY INPUT-SCRN
016100     ACCEPT INPUT-STUDENT-ID-SCRN
016200     PERFORM UNTIL WS-STUDENT-ID = ZEROS OR WS-CONT = 'N'
016300         ACCEPT INPUT-FNAME-SCRN
016400         ACCEPT INPUT-LNAME-SCRN
016500         ACCEPT INPUT-STREET-SCRN
016600         ACCEPT INPUT-ZIP-SCRN
016700
016800*Look for the zip
016900
017000
017100         MOVE WS-ZIP TO IN-ZIP
017200
017300         MOVE 'N' TO WS-ERR
017400
017500         START INZIP KEY = IN-ZIP
017600             INVALID KEY
017700                 MOVE 'Y' TO WS-ERR
017800         END-START
017900
018000         IF NOT WS-ERR = 'Y'
018100
018200*get the zip record and accept the active
018300
018400             READ INZIP NEXT RECORD
018500             END-READ
018600
018700             DISPLAY INPUT-WITH-ZIP-SCRN
018800             ACCEPT INPUT-ACTIVE-SCRN
018900
019000             MOVE WS-STUDENT-ID      TO OUT-STUDENT-ID
019100             MOVE WS-FNAME           TO OUT-FNAME
019200             MOVE WS-LNAME           TO OUT-LNAME
019300             MOVE WS-STREET          TO OUT-STREET
019400             MOVE WS-ZIP             TO OUT-ZIP
019500             MOVE WS-ACTIVE          TO OUT-ACTIVE
019600
                   DISPLAY ADD-SCRN
                   ACCEPT ADD-SCRN

                   IF WS-ADD = 'Y'
                       MOVE 'N' TO WS-ERR

                       WRITE STUDENT-RECORD
                           INVALID KEY MOVE 'Y' TO WS-ERR
                       END-WRITE

                       IF WS-ERR = 'Y'
                           DISPLAY ERROR-SCRN
                           ACCEPT ERROR-SCRN
                       ELSE
                           DISPLAY SUCCESS-SCRN
                           ACCEPT SUCCESS-SCRN
                       END-IF
                   ELSE
                       DISPLAY CONTINUE-SCRN
                       ACCEPT CONTINUE-SCRN
                   END-IF

021100         ELSE
021200             DISPLAY ERROR-ZIP-SCRN
021300             ACCEPT ERROR-ZIP-SCRN
021400         END-IF
021500
021600
021700         IF WS-CONT = 'Y'
021800             DISPLAY INPUT-SCRN
021900             MOVE ZEROS           TO OUT-STUDENT-ID
022000             MOVE SPACES          TO OUT-FNAME
022100             MOVE SPACES          TO OUT-LNAME
022200             MOVE SPACES          TO OUT-STREET
022300             MOVE SPACES          TO OUT-ZIP
022400             MOVE SPACES          TO OUT-ACTIVE
022500             ACCEPT INPUT-STUDENT-ID-SCRN
022600         END-IF
022700     END-PERFORM
022800     CLOSE OUTFILE
022900     CLOSE INZIP
023000
023100     EXIT PROGRAM.
